package com.gitee.leijin.rbac.util;

import com.gitee.leijin.rbac.enums.CodeEnum;
import lombok.Data;
import org.aspectj.apache.bcel.classfile.Code;

@Data
public class Result<T> {

    private Integer code = CodeEnum.SUCCESS.getCode();

    private String message = CodeEnum.SUCCESS.getMessage();

    private T data;

    private Result() {}

    private Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> success() {
        return new Result<>();
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failure() {
        return new Result<>(CodeEnum.FAIL.getCode(), CodeEnum.FAIL.getMessage());
    }

    public static <T> Result<T> failure(CodeEnum codeEnum) {
        return new Result<>(codeEnum.getCode(), codeEnum.getMessage());
    }

    public static <T> Result<T> failure(String message) {
        return new Result<>(CodeEnum.FAIL.getCode(), message);
    }

    public static <T> Result<T> failure(Integer code, String message) {
        return new Result<>(code, message);
    }

    public boolean isSuccess() {
        return CodeEnum.SUCCESS.getCode().equals(code);
    }
}
